package com.qf.dao.impl;

import com.alibaba.druid.pool.DruidDataSource;
import com.qf.dao.ProductDao;
import com.qf.pojo.Product;
import com.qf.utils.DruidJdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 高圆圆
 * @date 2023/4/23 14:36
 * 商品的数据访问接口实现
 */
public class ProductDaoImpl  implements ProductDao {
    /**
     * 数据访问接口根据商品的上架日期查询最新商品
     * @return   返回商品列表
     */
    @Override
    public List<Product> selectNewProducts() throws SQLException {
        //执行对象
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        //sql
        String sql = "SELECT * FROM product ORDER BY pdate DESC LIMIT 9 ;" ;
        //查询
        List<Product> productList = qr.query(sql, new BeanListHandler<>(Product.class));

        return productList;
    }


    @Override
    public List<Product> selectHotProducts() throws SQLException {
        //执行对象
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        //sql
        String sql = "SELECT * FROM product WHERE is_hot = 1 LIMIT 9 " ;
        //查询
        List<Product> productList = qr.query(sql, new BeanListHandler<>(Product.class));

        return productList;
    }

    /**
     * 数据访问接口根据商品id查询指定的商品实体
     * @param pid 商品编号
     * @return 返回商品实体
     */
    @Override
    public Product selectProductByPid(String pid) throws SQLException {

        //执行对象
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        //sql
        String sql = "select * from product where pid = ?" ;
        //查询
        Product prodcut = qr.query(sql, new BeanHandler<>(Product.class), pid);
        return prodcut;
    }

    /**
     * 数据访问接口查询商品分页数据
     * @param start    起始索引=(当前页码-1)*每页显示的条数
     * @param pageSize  每页显示的条数
     * @param cid      分类id
     * @return     返回商品列表数据
     */
    @Override
    public List<Product> selectProductByPage(int start, int pageSize, String cid) throws SQLException {
        //执行对象
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        //sql
        String sql = "select * from product where cid = ? limit ?,?" ;
        //查询
        List<Product> productList = qr.query(sql, new BeanListHandler<>(Product.class), cid, start, pageSize);
        return productList;
    }

    /**
     * 获取商品的总记录数根据商品分类id
     * @param cid 分类id
     * @return 返回总记录数
     */
    @Override
    public int totalCount(String cid) throws SQLException {
        //执行对象
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        //sql
        String sql = "select count(pid) from product where cid = ?" ;
        Object obj = qr.query(sql, new ScalarHandler<>(), cid);
        int totalCount = Integer.parseInt(String.valueOf(obj)) ;
        return totalCount;
    }
}
